#include "Link.h"
#include <iostream>
using namespace std;

Link::Link()
{
	//创建头节点
	this->head = new Node();
	head->next = head;
	head->prev = head;
}

Link::~Link()
{
	//释放所有链表节点
	Node* p = head->next;
	while(p != head){
		p = p->next;
		delete p->prev;
	}

	delete head;
}

void Link::insertNode(int value)
{
	//创建新节点
	Node* newNode = new Node();
	newNode->value = value;

	//链接新节点到链表中
	//先链接新节点的指针域
	newNode->next = head;
	newNode->prev = head->prev;

	head->prev->next = newNode;
	head->prev = newNode;
}

void Link::deleteNode(int value)
{
	Node* p = head->next;
	while(p != head){
		if(p->value == value){
			Node* q = p->prev;
			//删除节点
			p->prev->next = p->next;
			p->next->prev = p->prev;
			delete p;
			p = NULL;
			p = q;
		}
		p = p->next;
	}
	

}

void Link::display() const
{
	Node* p = head->next;
	while(p != head){
		cout << p->value << endl;
		p = p->next;
	}
	
}


